﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Telephony;

namespace C4F.DevKit.Telephony.Controls
{
    [ToolboxItem(true), ToolboxBitmap(typeof(DTMFPanel))]
    public partial class DTMFPanel : UserControl
    {
        #region Members

        private TapiManager _tapiManager;

        #endregion

        #region Properties

        public TapiManager TapiManager
        {
            get
            {
                return _tapiManager;
            }

            set
            {
                _tapiManager = value;
            }
        }

        [Browsable(true)]
        public bool DtmfEnabled
        {
            get
            {
                return groupBoxDigitDetection.Enabled;
            }

            set
            {
                groupBoxDigitDetection.Enabled = value;
                groupBoxDigitGeneration.Enabled = value;
            }
        }

        #endregion

        #region Constructors

        public DTMFPanel()
        {
            InitializeComponent();
            chkStartDigitDetection.CheckedChanged +=new EventHandler(chkStartDigitDetection_CheckedChanged);
            btnClear.Click += new EventHandler(btnClear_Click);
            btnGenerate.Click += new EventHandler(btnGenerate_Click);
            txtDigitDetection.TextChanged += new System.EventHandler(this.txtDigitDetection_TextChanged);
        }

        #endregion

        #region Methods

        public void ChangeStateOfClearButton()
        {
            if (txtDigitDetection.Text != string.Empty)
            {
                btnClear.Enabled = true;
            }
            else
            {
                btnClear.Enabled = false;
            }
        }

        public void DigitDetectionTextBoxUpdate(object digitObject)
        {
            char digit = (char)digitObject;
            txtDigitDetection.Text += digit.ToString();
        }

        #endregion

        #region Event Handlers

        private void chkStartDigitDetection_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkDigit = (CheckBox)sender;
            if (chkDigit.Checked == true)
            {
                txtDigitDetection.Enabled = true;

                ChangeStateOfClearButton();
                try
                {
                    _tapiManager.StartDigitDetection();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                txtDigitDetection.Enabled = false;
                btnClear.Enabled = false;
                try
                {
                    _tapiManager.StopDigitDetection();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDigitDetection.Text = string.Empty;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.GenerateDigits(txtDigitGeneration.Text);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtDigitDetection_TextChanged(object sender, EventArgs e)
        {
            ChangeStateOfClearButton();
        }

        #endregion
    }
}
